package com.sti.framework.models.Basics.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sti.framework.core.annotation.authorization.Authorization;
import com.sti.framework.core.controller.BaseController;
import com.sti.framework.core.web.Page;
import com.sti.framework.core.web.ResponseBodyEntity;
import com.sti.framework.models.Basics.data.BasicsParameter;
import com.sti.framework.models.Basics.data.BasicsParameterSearchWhere;
import com.sti.framework.models.Basics.data.BasicsParameterValue;
import com.sti.framework.models.Basics.mapper.BasicsParameterMapper;
import com.sti.framework.models.Basics.service.BasicsParameterService;
import com.sti.framework.models.Basics.service.BasicsParameterValueService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 模块： 基础设置 Basics
 * 表格： 事务参数 aps_parameter
 *
 * @author AutoApi
 */
@RestController
@RequestMapping("/Api/Parameter")
@Api(tags = "事务参数", value = "基础设置")
public class BasicsParameterController extends BaseController<BasicsParameterMapper, BasicsParameter, BasicsParameterSearchWhere> {


    @Resource
    private BasicsParameterService basicsParameterService;
    @Resource
    private BasicsParameterValueService basicsParameterValueService;

    /**
     * 列表
     *
     * @param page        分页内容
     * @param searchWhere 搜索内容
     * @return
     */
    @GetMapping("/ListPage")
    public ResponseBodyEntity listPage(Page page, BasicsParameterSearchWhere searchWhere) {
        LambdaQueryWrapper<BasicsParameter> queryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotEmpty(searchWhere.getParameterName())){
            queryWrapper.like(BasicsParameter::getParameterName, searchWhere.getParameterName());
        }
        if(StringUtils.isNotEmpty(searchWhere.getDescription())){
            queryWrapper.like(BasicsParameter::getDescription, searchWhere.getDescription());
        }
        queryWrapper.orderByDesc(BasicsParameter::getCreatedDate);
        return ResponseBodyEntity.success(basicsParameterService.getListPage(page, queryWrapper));
    }

    /**
     * 详情
     */
    @GetMapping("/InfoData")
    public ResponseBodyEntity infoData(Long id) {
        if(id == null){
            return ResponseBodyEntity.fail(ResponseBodyEntity.ResponseStatus.FAIL,"缺少主键");
        }
        BasicsParameter data = basicsParameterService.get(id);
        // 数据不存在
        if (data == null){
            return ResponseBodyEntity.fail(ResponseBodyEntity.ResponseStatus.USER_ERROR_22101);
        }
        //如果是多值则需要获取下面数据
        if("1".equals(data.getCategory())){
            LambdaQueryWrapper<BasicsParameterValue> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(BasicsParameterValue::getParameterId, data.getId());
            lambdaQueryWrapper.orderByDesc(BasicsParameterValue::getSequence).orderByDesc(BasicsParameterValue::getCreatedDate);
            data.setBasicsParameterValueList(basicsParameterValueService.list(lambdaQueryWrapper));
        }
        return ResponseBodyEntity.success(data);
    }
}
